package gen

import (
	"fmt"
	"gitee.com/pkg/root"
	v1 "gitee.com/runx/gitctl/pkg/api/v1"
	"gitee.com/runx/gitctl/pkg/cli"
	"github.com/spf13/cobra"
	"io/ioutil"
	"os"
	"strings"
)

var (
	xFile string
	xOut string
	xCli  *cli.Cli
	dmCmd = &cobra.Command{
		Use:   "gen",
		Short: "gen file info gitea",
		RunE: func(c *cobra.Command, args []string) error {
			res, err := v1.Of(xFile)
			if err != nil {
				fmt.Printf("read file error,reson:%s\n", err)
			}
			outDir:=fmt.Sprintf("%s/%ss/%s", xOut, strings.ToLower(res.Kind),res.Metadata.Namespace)
			if _, err := os.Stat(outDir); err != nil {
				os.MkdirAll(outDir,0755)
			}
			api := fmt.Sprintf("%s/%s.yaml", outDir, res.Metadata.Name)
            ioutil.WriteFile(api,[]byte(res.Content),0755)
			return nil
		},
	}
)

func init() {
	dmCmd.PersistentFlags().StringVarP(&xFile, "file", "f", "", "apply file into gitee")
	dmCmd.PersistentFlags().StringVarP(&xOut, "o", "c", "/tmp/.cdcache/.local", "tool cache dir ")
	root.AddCmd(dmCmd)
}